const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';

const createInboundShipmentPlan = {};
createInboundShipmentPlan.generateOption = function(seller_id, mws_auth_token, shipFromAddress, inboundShipmentPlanRequestItems, shipToCountryCode=null, shipToCountrySubdivisionCode=null, labelPrepPreference=null){
  let baseOption = {
    'Version': '2010-10-01',
    'Action': 'CreateInboundShipmentPlan',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
  };

  if (shipToCountryCode)
    baseOption['ShipToCountryCode'] = shipToCountryCode;
  if (shipToCountrySubdivisionCode)
    baseOption['ShipToCountrySubdivisionCode'] = shipToCountrySubdivisionCode;
  if (labelPrepPreference)
    baseOption['LabelPrepPreference'] = labelPrepPreference;

  const option = Object.assign(baseOption,
    jsonflatten.flatten(shipFromAddress, null, baseIndex=1),
    jsonflatten.flatten(inboundShipmentPlanRequestItems, null, baseIndex=1));
  return option;
};

createInboundShipmentPlan._ = function(options) {
  if (!isProduction) {
    return amazonMws.fulfillmentInboundShipment.search(options);
  } else {
    console.error("bot not implemented!")
  }
};

createInboundShipmentPlan.createInboundShipmentPlan = function(seller_id, mws_auth_token, shipFromAddress, inboundShipmentPlanRequestItems, shipToCountryCode=null, shipToCountrySubdivisionCode=null, labelPrepPreference=null) {
  return this._(this.generateOption(seller_id, mws_auth_token, shipFromAddress, inboundShipmentPlanRequestItems, shipToCountryCode, shipToCountrySubdivisionCode, labelPrepPreference));
};

module.exports = createInboundShipmentPlan;